class Solution {
    public:
        int maxTurbulenceSize(vector<int>& arr) {
            int n = arr.size();
            vector<int> dp(n);//dp[i]表示以i位置为结尾的最长湍流子长度
            if(n == 1) return 1;
            dp[0] = 1;
            dp[1] = arr[0] == arr[1] ? 1 : 2;
            int ret = dp[1];
            for(int i = 2 ; i < n ; i++){
                int diff1 = arr[i-2]-arr[i-1];
                int diff2 = arr[i-1]-arr[i];
                if((diff1 >0 && diff2 <0) || (diff1<0 && diff2>0)){
                    dp[i] = dp[i-1] +1;
                }else if((diff1>0&&diff2>0)||(diff1<0&&diff2<0)){
                    dp[i] = 2;
                }else{
                    if(diff2 == 0){
                        dp[i] = 1;
                    }else{
                        dp[i] = 2;
                    }
                }
                ret = max(ret,dp[i]);
            }
            return ret;
        }
    };